[#include "/includes/_layout.html"/]
<title>设置奖品跟概率</title>
</head>
<body class="fixed-sidebar full-height-layout gray-bg">
[@layout]
<div class="wrapper wrapper-content">
	<div class="row animated fadeInLeft">
	
	[#include "/includes/menu-interaction.html" /]
	
	<div class="col-sm-11">
	<div class="ibox-title row">
		<h5><small><a href="${webctx}/lottery">《返回列表</a> /创建刮刮乐抽奖</small></h5>
	</div>
<div class="tabs-container row" style="padding-top: 12px;">
        <ul class="nav nav-tabs">
		<li><a href="${webctx}/lottery/show/guaguale/${lottery.id}">编辑抽奖信息</a></li>
		<li class="active"><a href="${webctx}/lottery/set/guaguale/${lottery.id}">设置奖品跟概率</a></li>
		<li><a href="${webctx}/lottery/test/${lottery.id}">模拟抽奖结果</a></li>
	</ul>
</div>
<div id="tableList" class="row ibox-content">
[#if lottery??]
<div style="margin-top: 20px;margin-left: 260px;">
    <a class="btn btn-large btn-success" id="btnAddAward" href="javascript:void(0);">+ 添加活动奖品</a>
	<div style="color: #aaa;font-size:14px;padding:4px 10px;" id="spanend">
		最多设置<span id="selAwd" style="color:green; font-weight:bold;font-size:22px">10</span>个奖品，
		已经设置<span id="selNoAwd" style="color:red;font-weight:bold;font-size:22px">0</span>个奖品
	</div>
</div>
<table id="sel_prize_table" class="table table-striped table-bordered bootstrap-datatable datatable" style="width: 60%;" align="center">
	<th>奖品名称</th>
	<th>中奖率</th>
	<th>剩余数量</th>
	<th>奖品类型</th>
	<th>操作</th>
    <tbody id="awardList"><tr><td style="text-align:center;" colspan="6">亲，您还没有设置奖品哦！</td></tr></tbody>
</table>
[#else]
<div class="alert alert-danger">
	<i class="fa fa-info-circle"></i><strong>警告:</strong>请先设置抽奖信息。
</div>
[/#if]
</div>
</div>
</div>
</div>
[#include "/lottery/prize_setting_dialog.html" /]
<script type="text/javascript">
function initAward(){
	obz.ajaxJson(obz.ctx + "/lottery/award/list", {lotteryId : '${lottery.id}'}, function(resp){
		if(resp.code == 200){
			var result = resp.data;
			if(result.length >0){
				$("#awardList").empty();
				for(var i=0;i<result.length;i++){
					var award = result[i];
					award.left_count = parseInt(award.publish_count) - parseInt(award.had_out_count);
					var trHtml = obz.dataTemplate4obj($("#had-prize-tr").html(), award);
					$("#awardList").append(trHtml);
				}
				$("#selNoAwd").html($("#sel_prize_table tr").length - 1);
			}
		}
	});
}
function cancelset(obj){
	obz.showMessage("确定删除奖品吗？", function(){
		obz.ajaxJson(obz.ctx+"/lottery/award/del", {id:$(obj).attr("aindex")}, function(resp){
			if(resp.code != 200){
				BootstrapDialog.alert({title:"提示",message:"亲，删除失败，请联系管理员。"});
				return;
			}
			location.reload();
		});
	});
}
function editAward(obj){
	obz.ajaxJson(obz.ctx+ "/lottery/award/show", {awardId:$(obj).attr("aid")}, function(resp){
		if(resp.code == 200){
			var aresult = resp.data;
			if(aresult != null){
				$("#id").val(aresult.id);
				$("#prize_id").val(aresult.prize_id);
				$("#award_name").val(aresult.award_name);
				$("#award_count").val(aresult.award_count);
				$("#award_rate").val(aresult.award_rate);
				$("#award_pos").val($("#selNoAwd").text());
				$("#setting_prize_dialog").modal('show');
			}
		}
	});
}

$(function(){
	initAward();
	$("#selectPrize").click(function(){
		obz.selectPrize({}, function (id, prizeName) {
			$("#id").val("");
			$("#prize_id").val(id);
			$("#award_name").val(prizeName);
			$("#error_prize_id").find("label").empty();
		});
	});	
	$('#btnAddAward').on('click',function(){
		var me = $(this);
		if(($("#sel_prize_table tr").length - 1)>=10){
			BootstrapDialog.alert({title:"温馨提示",message:"亲，您设置奖品达到上限啦！删除部分奖品后再添加奖品吧。"});
			return false;
		}
		$("#award_pos").val(parseInt($("#selNoAwd").text()) + 1);
		$("#setting_prize_dialog").modal('show');
	});
	$('#setting_prize_dialog').on('hidden.bs.modal', function () {
		$("#id,#prize_id,#award_name,#award_rate,#award_count").val("");
	});
	$("#setBut").click(function(){
		var params = {}, error = {};
		var id = $("#id").val(), prize_id = $("#prize_id").val(),
		award_name = $("#award_name").val(), award_count = $("#award_count").val(), award_rate = $("#award_rate").val();
		if($.trim(prize_id) =="") {error.error_prize_id = "请选择奖品"; } else {error.error_prize_id=""; params.prize_id = $.trim(prize_id);}
		if($.trim(award_name) =="") {error.error_award_name = "请填写奖品名称"; } else {error.error_award_name =""; params.award_name = $.trim(award_name);}
		if($.trim(award_rate) =="") {error.error_award_rate = "请填写中奖概率"; } else { error.error_award_rate=""; params.award_rate = $.trim(award_rate);}
		if($.trim(award_count) =="") {error.error_award_count = "请填写奖品数量"; } else { error.error_award_count=""; params.award_count = $.trim(award_count);}
		if($.trim(id) != "") params.id = id;
		params.lottery_id = '${lottery.id}';
		params.award_pos = $("#award_pos").val();
		var hasError = false;
		for(var key in error){
			if(error[key]!=""){
				if(!hasError) hasError = true;
				$("#"+key).addClass("has-error");
    			$("#"+key).find("label").text(error[key]);
			}else{
				$("#"+key).removeClass("has-error");
				$("#"+key).find("label").empty();
			}
		}
		if(hasError) return false;
		obz.ajaxJson(obz.ctx+"/lottery/award/save", params, function(data){
			if(data.code != 200){
				BootstrapDialog.alert({title:"提示",message: data.msg});
				return;
			}
			$("#setting_prize_dialog").modal('hide');
			//设置奖品信息
			initAward();
		});
	});
});

</script>
<script type="text/template" id="had-prize-tr">
<tr><td>{award_name}</td><td>{award_rate}%</td><td>{left_count}</td><td>{type_name}</td><td><a class="btn-small" aid="{id}" onclick="editAward(this)" href="javascript:void(0);">修改</a>&nbsp;<a class="btn-small" onclick="cancelset(this)" aindex="{id}" href="javascript:void(0);">删除</a></td></tr>
</script>
[/@layout]
<script>
Template.init("#menu-qiandao");
Template.initSecond("#menu-draw");
</script>
</body>
</html>	
	
